package org.jeecg.modules.business.strategy.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusLivingLayoutDayPrice;
import org.jeecg.modules.business.entity.BusOrderFee;
import org.jeecg.modules.business.entity.BusRoomsLivingOrder;
import org.jeecg.modules.business.enums.FeeSubjectType;
import org.jeecg.modules.business.enums.FeeType;
import org.jeecg.modules.business.service.IBusLivingLayoutDayPriceService;
import org.jeecg.modules.business.service.IBusOrderFeeService;
import org.jeecg.modules.business.service.IBusRoomsLivingOrderService;
import org.jeecg.modules.business.service.IBusSchemeLayoutDailyPriceService;
import org.jeecg.modules.rooms.entity.CesAllDayPriceRule;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Component
public class PricingUtil {

    @Resource
    private IBusLivingLayoutDayPriceService livingLayoutDayPriceService;

    @Resource
    IBusSchemeLayoutDailyPriceService schemeLayoutDailyPriceService;

    @Resource
    IBusRoomsLivingOrderService roomsLivingOrderService;

    @Resource
    IBusOrderFeeService feeService;

    /**
     * 完整费用时间
     * @return
     */
    public Date getFullFeeTime(Date date, CesAllDayPriceRule cesAllDayPriceRule){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
        String endDateStr = dateFormat.format(date);
        Date endTime = null;
        try {
            endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(endDateStr + cesAllDayPriceRule.getEndTime());
        } catch (ParseException ex) {
            throw new JeecgBootException("结束时间格式异常");
        }
        return endTime;
    }

    /**
     * 首次超时收费时间
     */
    public Calendar getRemindTime(BusRoomsLivingOrder livingOrder, CesAllDayPriceRule cesAllDayPriceRule, Integer BookingType){
        Calendar calendar = Calendar.getInstance();
        if (ObjectUtil.equals(BookingType, 1)) {
            calendar.setTime(livingOrder.getArrivalTime());
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Calendar overDate = Calendar.getInstance();
            overDate.setTime(livingOrder.getDueOutTime());
            calendar.set(Calendar.HOUR_OF_DAY, overDate.get(Calendar.HOUR_OF_DAY));
            calendar.set(Calendar.MINUTE, overDate.get(Calendar.MINUTE));
            calendar.add(Calendar.MINUTE, Integer.parseInt(cesAllDayPriceRule.getDayTime()));
        }else if(ObjectUtil.equals(BookingType, 2)){
            // TODO: 2023/10/24 钟点房超时时间
            calendar.setTime(livingOrder.getDueOutTime());
        }
        return calendar;
    }


    public BusLivingLayoutDayPrice addDayPrice(BusRoomsLivingOrder livingOrder, BusBookingRooms bookingroom, String layoutId){
        List<BusLivingLayoutDayPrice> dayPrices = livingLayoutDayPriceService.list(Wrappers.<BusLivingLayoutDayPrice>query()
                .eq("living_order_id",livingOrder.getId()));
        Optional<BusLivingLayoutDayPrice> opPrice = dayPrices.stream().filter(a-> DateUtils.formatDate(a.getDayTime()).equals(DateUtils.formatDate(livingOrder.getDueOutTime()))).findFirst();
        if(!opPrice.isPresent()){
            // 根据房价方案查询价格
            BigDecimal money = schemeLayoutDailyPriceService.getOneDayPrice(layoutId, livingOrder, new Date());
            BusLivingLayoutDayPrice nPrice = new BusLivingLayoutDayPrice();
            nPrice.setBookingRoomId(livingOrder.getBookingRoomId());
            try {
                nPrice.setDayTime(DateUtils.parseDate(DateUtils.formatDate(new Date()),"yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            nPrice.setRoomId(bookingroom.getRoomId());
            nPrice.setRoomLayoutId(bookingroom.getRoomLayoutId());
            nPrice.setPrice(money);
            nPrice.setLivingOrderId(livingOrder.getId());
            nPrice.setLivingType(livingOrder.getLivingType());
            livingLayoutDayPriceService.save(nPrice);
            return nPrice;
        }
        return opPrice.get();
    }

    public Calendar getNextCalendar(BusRoomsLivingOrder livingOrder, Calendar nowCalendar,CesAllDayPriceRule cesAllDayPriceRule) {
        Calendar nextCalendar = Calendar.getInstance();
        nextCalendar.setTime(livingOrder.getDueOutTime());
        nextCalendar.set(Calendar.YEAR, nowCalendar.get(Calendar.YEAR));
        nextCalendar.set(Calendar.MONTH, nowCalendar.get(Calendar.MONTH));
        nextCalendar.set(Calendar.DAY_OF_MONTH, nowCalendar.get(Calendar.DAY_OF_MONTH));
        nextCalendar.add(Calendar.DAY_OF_YEAR,1);
        // 修改预离时间
        if(nextCalendar.getTime().compareTo(livingOrder.getDueOutTime()) > 0){
            livingOrder.setDueOutTime(nextCalendar.getTime());
            roomsLivingOrderService.updateById(livingOrder);
        }
        // 下一次收费时间戳
        nextCalendar.add(Calendar.MINUTE, Integer.parseInt(cesAllDayPriceRule.getDayTime()));
        return nextCalendar;
    }

    public BusOrderFee getOverTimeFee(BusRoomsLivingOrder livingOrder, BusBookingRooms bookingroom) {
        List<BusOrderFee> feeList = feeService.list(Wrappers.<BusOrderFee>lambdaQuery()
                .eq(BusOrderFee::getLivingOrderId, livingOrder.getId()).eq(BusOrderFee::getPreferentialStatus, 1)
                .eq(BusOrderFee::getSubjectType, FeeSubjectType.MEI_RI_FANG_FEI.getKey()).eq(BusOrderFee::getIsOverTime, true)
                .orderByDesc(BusOrderFee::getCreateTime));
        if (CollUtil.isNotEmpty(feeList)) {
            return feeList.get(0);
        }
        BusOrderFee fee = new BusOrderFee();
        fee.setMoney(new BigDecimal(0));
        fee.setCreateTime(new Date());
        fee.setDayTime(new Date());
        fee.setSubjectType(FeeSubjectType.MEI_RI_FANG_FEI.getKey());
        fee.setLivingOrderId(livingOrder.getId());
        fee.setHotelId(livingOrder.getHotelId());
        fee.setFeeType(FeeType.CONSUME.getKey());
        fee.setRoomId(bookingroom.getRoomId());
        fee.setRemark("超时房费");
        fee.setIsOverTime(true);
        return fee;
    }

}
